package com.app.controller;


import com.app.common.Result;
import com.app.model.Bank;
import com.app.model.UserModel;
import com.app.service.IBankService;
import com.app.service.UserService;
import com.app.utils.IpUtil;
import com.app.utils.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.binarywang.wxpay.service.BankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 银行卡表 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2024-09-03
 */
@RestController
@RequestMapping("/bank")
public class BankController {
    @Autowired
    private IBankService bankService;
    @Autowired
    private UserService userService;




    /**
     * 根据用户ID查询用户对应的银行、收款码信息列表
     */
    @GetMapping("/getBankList")
    public Result getByBankId(@RequestParam(value = "page",defaultValue = "1") Integer page,
                              @RequestParam(value = "limit",defaultValue = "10") Integer limit,
                              HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        String ipAddress = IpUtil.getIpAddress(request);
        userModel.setIp(ipAddress);
        return Result.success(bankService.getBankList(page,limit,userModel.getId()));
    }




    @GetMapping("/del")
    public Result del(@RequestParam(value = "id") Integer id,
                              HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        QueryWrapper<Bank> bankQueryWrapper=new QueryWrapper<>();
        bankQueryWrapper.eq("user_id",userModel.getId());
        bankQueryWrapper.eq("id",id);
        Bank bank=this.bankService.getOne(bankQueryWrapper);
        if(bank!=null)
        {
            bank.setStatus(0);
            return Result.success(this.bankService.updateById(bank));
        }

        return Result.success(null);
    }



    /**
     * 新增
     */
    @PostMapping("/addBank")
    public Result addBank(@RequestBody Bank bank, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        return bankService.addBank(bank,userModel);
    }



}

